define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    $('#choose-shelves').click(function(){
        let house_id = $("#c-warehouse_id").val();
        if(!house_id){
            Backend.api.toastr.error('请先选择仓库');
            return false;
        }
        top.Fast.api.open('information/shelves/choose?warehouse_id='+house_id,'选择货架',{offset: 'r',callback:function(value){
                $("#c-shelves_id").val(value.id);
                 $("#c-shelves_id").selectPageRefresh();
            }});
    })

    $('#choose-shelves-change').click(function(){
        let house_id = $("#c-warehouse_id").val();
        if(!house_id){
            Backend.api.toastr.error('请先选择仓库');
            return false;
        }
        top.Fast.api.open('information/shelves/choose?warehouse_id='+house_id,'选择货架',{offset: 't',callback:function(value){
                $("#c-shelves_change_id").val(value.id);
                //alert(value);
                $("#c-shelves_change_id").selectPageRefresh();
            }});
    })
    $('#btn-add-same').click(function(){
        Fast.api.open('outhouse/tpdj/same','添加同库调拨单',{area:['100%','100%'],callback:function(value){
            }});
    })


    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'outhouse/tpdj/index' + location.search,
                    add_url: 'outhouse/tpdj/add',
                    edit_url: 'outhouse/tpdj/edit',
                    del_url: 'outhouse/tpdj/del',
                    // multi_url: 'outhouse/tpdj/multi',
                    import_url: 'outhouse/tpdj/import',
                    table: 'tpdj',
                }
            });
            Fast.config.openArea = ['100%','100%'];
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                //pk: 'id',
                //sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 4,
                columns: [
                    [
                        {checkbox: true},
                    //    {field: 'id', title: __('Id')},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'code_date', title: __('Code_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},

                        {field: 'warehouse_name', title: __('Warehouse_id'), operate: 'LIKE'},
                        {field: 'shelves_name', title: __('Shelves_id'), operate: 'LIKE'},
                        {field: 'warehouse_change_name', title: __('Warehouse_change_id'), operate: 'LIKE'},
                        {field: 'shelves_change_name', title: __('Shelves_change_id'), operate: 'LIKE'},

                        {field: 'inv_number', title: __('Cinvcode'), operate: 'LIKE'},
                        {field: 'inv_name', title: __('Name'), operate: 'LIKE'},
                        {field: 'inv_specification', title: __('Specification'), operate: 'LIKE'},
                        {field: 'cBatch', title: __('CBatch'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'unit', title: __('Unit'), operate: 'LIKE'},
                        {field: 'quantity', title: __('Iquantity'), operate:'BETWEEN'},
                        {field: 'store_unit', title: __('Store_unit'), operate: 'LIKE'},
                        {field: 'store_quantity', title: __('Store_quantity'), operate:'BETWEEN'},
                        
                        
                        // {field: 'warehouse_id', title: __('Warehouse_id'),formatter:function(val,row,i){
                        //         return row.tpdj.ware_house.name;
                        //     },searchList:$.getJSON('ajax/getWarehouseDataSearchList')},
                        // {field: 'warehouse_change_id', title: __('Warehouse_change_id'),formatter:function(val,row,i){
                        //         return row.tpdj.change_ware_house.name;
                        //     },searchList:$.getJSON('ajax/getWarehouseDataSearchList')},
                        // {field: 'shelves_id', title: __('Shelves_id'),formatter:function(val,row,i){
                        //         if(row.shelves_id!=0){
                        //             return row.shelves.name;
                        //         }

                        //     },operate:false},
                        // {field: 'shelves_change_id', title: __('Shelves_change_id'),formatter:function(val,row,i){
                        //         if(row.shelves_change_id != 0){
                        //             return row.change_shelves.name;
                        //         }
                        //  },operate: false},
                        // {field: 'tpdj.status', title: '状态', searchList: {"0":'关闭',"1":'开启'}, formatter:function(varl,row,i){
                        //         if(row.tpdj.status==1){
                        //             return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="" data-field="status" data-value="1" data-original-title="点击搜索 开启"><span class="text-success">开启</span></a>';
                        //         }else{
                        //             return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="" data-field="status" data-value="0" data-original-title="点击搜索 关闭"><span class="text-primary">关闭</span></a>';
                        //         }
                        //     }},
                        
                       // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       /*{field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'check',
                                    text: '锁单',
                                    title: '锁单',
                                    icon: 'fa fa-magic',
                                    confirm: '确定锁单',
                                    classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                    url: 'outhouse/tpdj/checkStatus',
                                    visible: function (row) {

                                        if(row.tpdj.status==0){
                                            return true;
                                        }
                                        //返回true时按钮显示,返回false隐藏
                                        return false;
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        // console.log(data, ret);
                                        Backend.api.toastr.error(ret.msg);
                                        return false;
                                    }
                                }],
                            events: Table.api.events.operate,
                           formatter:function (value, row, index) {
                               var that = $.extend({},this);
                               var table = $(this.table).clone(true);
                               //后端传之判断是否有编辑、删除操作权限
                               if(row.tpdj.status==1){
                                   $(table).data('operate-edit',null);
                                   $(table).data('operate-del',null);
                               }

                               that.table = table;
                               return Table.api.formatter.operate.call(that,value,row,index);
                           }
                        }*/
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },


        add: function () {
            $(".btn-refresh").trigger("click");
            Controller.api.bindevent();
        },
        same: function () {
            $(".btn-refresh").trigger("click");
            Controller.api.bindevent();
        },
        edit: function () {
            $("#free-programme-item-table").trigger("fa.event.refreshfieldlist");
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
$('#choose-goods').click(function(){
    Fast.api.open('inventory/kc/choose','选择库存',{offset: 'r',callback:function(value){

            $("#c-basic-textarea").val(JSON.stringify(value));
            $("#free-programme-item-table").trigger("fa.event.refreshfieldlist");
        }});
})
function selectshelves(obj){
    let house_id = $("#c-warehouse_id").val();
    let this_obj = $(obj);
    let this_id_obj = this_obj.siblings('.c-shelves_id');
    let this_name_obj = this_obj.siblings('.c-shelves_name');
    top.Fast.api.open('information/shelves/choose?warehouse_id='+house_id,'选择货架',{offset: 'r',callback:function(value){
            // $("#c-shelves_id").val(value);
            this_id_obj.attr('value',value.id);
            this_name_obj.val(value.name);
            this_name_obj.selectPageRefresh();
        }});
}